package cn.qdhlm.collectservice.common.config;



import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Name: MqttConfig
 * @Description: 异步客户端：MqttAsyncClient
 * @Author: jiangcs
 */
@Configuration
public class MqttConfig {
    @Value("${mqtt.uri}")
    private String uri;
    @Value("${mqtt.clientId}")
    private String clientId;

//    @Autowired
//    private MqttReceiveCallback mqttReceiveCallback;

    @Bean
    public MqttAsyncClient mqttClient() {
        try {
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setAutomaticReconnect(true);

            MqttAsyncClient client = new MqttAsyncClient(uri, clientId + System.currentTimeMillis(), new MemoryPersistence());
//            client.setCallback(mqttReceiveCallback);
            client.connect(options);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
